package com.chineseall.rebirth.bridge.zedis;

import java.util.List;

import jmind.core.redis.Redis;
import jmind.resource.manager.ZedisManager;

import com.alibaba.fastjson.JSON;

/**
 * zedis 服务适配
 * @author wbxie
 * 2013-11-20
 */
public class ZedisAdapter {

    public static Redis getEnv(String env) {

        return ZedisManager.getInstance().getResource(env);
    }

    public static String get(String env, String key) {
        return getEnv(env).get(key);
    }

    public static boolean isError(String text) {
        return text == null || "".equals(text.trim()) || text.replace("\"", "'").contains("'result': 'error'");
    }

    public static <T> T get(String env, String key, Class<T> clazz) {
        String text = get(env, key);
        if (isError(text))
            return null;
        return JSON.parseObject(text, clazz);
    }

    public static <T> List<T> getList(String env, String key, Class<T> clazz) {
        String text = get(env, key);
        if (isError(text))
            return null;
        return JSON.parseArray(text, clazz);
    }

    public static String post(String env, String key, String val) {
        return getEnv(env).set(key, val);
    }

}
